package edu.BillME.datapars;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
/* @auther Siddharth Patel & Susan Foster */
public class JSONFunctions {

public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
private static HttpClient httpclient;

public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = getHttpClient();
            HttpPost request = new HttpPost(url);
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(formEntity);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();

            String result = sb.toString();
            return result;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }}}}
            

            private static HttpClient getHttpClient() {
                
            httpclient = new DefaultHttpClient();
                    final HttpParams params = httpclient.getParams();
                    HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
                    HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
                    ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
                
                return httpclient;
            }
            
            
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
   try{
           httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost(url);
           HttpResponse response = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();
           is = entity.getContent();

   }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
   }
   
 //convert response to string
   try{
           BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
           StringBuilder sb = new StringBuilder();
           String line = null;
           while ((line = reader.readLine()) != null) {
                   sb.append(line + "\n");
                   Log.e("line_check",line);
           }

           result=sb.toString();
           //reader.close();
           is.close();
   }catch(Exception e){
           Log.e("log_tag", "Error converting result "+e.toString());
   }
   
   try{
   
            jArray = new JSONObject(result);            
   }catch(JSONException e){
           Log.e("JSON fuction", "Error parsing data "+e.toString());
   }
    
   return jArray;
}
}

